package com.goods.controller.business.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.common.enums.buisiness.BizUserTypeEnum;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.utils.ListPageUtils;
import com.goods.controller.business.mapper.ProductCategoryMapper;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import com.goods.controller.business.converter.ProductCategoryConverter;
import com.goods.controller.business.mapper.ProductMapper;
import com.goods.controller.business.service.ProductCategorySercice;
import lombok.SneakyThrows;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class ProductCategorySerciceImpl implements ProductCategorySercice {

    @Autowired
    ProductCategoryMapper productCategoryMapper;

    @Autowired(required = false)
    ProductMapper productMapper;

    /**
     * 商品类别列表
     * @param pageNum
     * @param pageSize
     * @param productCategoryVO
     * @return
     */
    @Override
    public PageVO<ProductCategoryTreeNodeVO> findProductCategoryList(Integer pageNum, Integer pageSize, ProductCategoryVO productCategoryVO) {
        //开始分页
        //PageHelper.startPage(pageNum,pageSize);

        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("sort asc");

        if( productCategoryVO.getName() != null && !"".equals(productCategoryVO.getName())){
            criteria.andLike("name","%"+productCategoryVO.getName()+"%");
        }
        List<ProductCategory> productCategoryList = productCategoryMapper.selectByExample(example);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = ProductCategoryConverter.converterToCategoryVO(productCategoryList);
        List<ProductCategoryTreeNodeVO> categoryTreeNodeVOS = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        if (pageNum!=null && pageSize!=null) {
            List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(categoryTreeNodeVOS, pageSize, pageNum);
            return new PageVO<>(categoryTreeNodeVOS.size(),page);
        }

        return new PageVO<ProductCategoryTreeNodeVO>(categoryTreeNodeVOS.size(),categoryTreeNodeVOS);

    }

    /*@Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {

        //开启分页
        PageHelper.startPage(pageNum,pageSize);

        Example example = new Example(ProductCategory.class);

        //获取一级列表分类
        List<ProductCategory> productCategorieList = productCategoryMapper.selectByExample(example);

        ArrayList<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = new ArrayList<>();
        if(!StringUtils.isEmpty(productCategorieList)){
            for (ProductCategory productCategory : productCategorieList) {
                Example example1 = new Example(ProductCategoryTreeNodeVO.class);
                example1.createCriteria().andEqualTo("pid",productCategory.getId());

                List<ProductCategory> productCategoryLisy = productCategoryMapper.selectByExample(example1);

                ArrayList<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOArrayList = new ArrayList<>();

                if(!StringUtils.isEmpty(productCategoryLisy)){
                    for (ProductCategory category : productCategoryLisy) {
                        //三级分类
                        Example example2 = new Example(ProductCategoryTreeNodeVO.class);
                        example2.createCriteria().andEqualTo("pid",productCategory.getId());
                        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(example2);

                        ArrayList<ProductCategory> productCategoryArrayList = new ArrayList<>();
                        if(!StringUtils.isEmpty(productCategories)){
                            for (ProductCategory productCategory1 : productCategories) {

                                ProductCategory productCategorys = new ProductCategory();
                                //工具类赋值
                                BeanUtils.copyProperties(productCategories,productCategorys);
                                //设置分类等级
                                productCategorys.setSort(3);

                                productCategoryArrayList.add(productCategorys);
                            }
                        }
                        //子类赋值

                    }
                }
            }
        }


        return null;
    }*/

    /**
     * 父级分类
     * @return
     */
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {

        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = ProductCategoryConverter.converterToCategoryVO(productCategoryList);
        return CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);
    }

    /**
     * 添加
     * @param productCategoryVO
     */
    @Override
    public void addProductCategory(ProductCategoryVO productCategoryVO) {

        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory);
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @Override
    public ProductCategoryVO edit(Long id) {

        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        return  ProductCategoryConverter.converterToProductCategoryVO(productCategory);
    }

    /**
     * 修改
     * @param productCategoryVO
     */
    @Override
    public void update(ProductCategoryVO productCategoryVO) {

        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }


    @Override
    public void delete(Long id) throws Exception {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        if(productCategory == null){
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"该分类不存在无法删除");
        }else {
            //检查是否存在子分类
            Example example = new Example(ProductCategory.class);
            example.createCriteria().andEqualTo("pid",id);

            int childernCount = productCategoryMapper.selectCountByExample(example);
            if(childernCount != 0){
                throw  new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"存在子节点,无法直接删除");
            }
            //是否存在物资引用
            Example o = new Example(Product.class);
            o.createCriteria().andEqualTo("oneCategoryId",id).orEqualTo("twoCategoryId",id).orEqualTo("threeCategoryId",id);

            if (productMapper.selectCountByExample(o) != 0){
                throw  new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"存在子节点,无法直接删除");
            }
            productCategoryMapper.deleteByPrimaryKey(id);

        }
    }

}
